﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="Algorithm.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

namespace IveelySE.Common
{
    /// <summary>
    ///   通用算法类
    /// </summary>
    public class Algorithm
    {
        /// <summary>
        ///   二分查找算法
        /// </summary>
        /// <param name="min"> 起始位置 </param>
        /// <param name="max"> 结束位置 </param>
        /// <param name="num"> 被查找数 </param>
        /// <param name="list"> 查找集合 </param>
        /// <returns> 返回坐标位置 </returns>
        public int BinarySearch(int min, int max, int num, SortedList<int> list)
        {
            if(min >= max)
            {
                return -1;
            }

            int mid = (min + max)/2;
            if(list[mid] == num)
            {
                return mid;
            }

            if(list[mid] < num)
            {
                return this.BinarySearch(mid + 1, max, num, list);
            }

            return this.BinarySearch(min, mid - 1, num, list);
        }
    }
}